package org.testis.data;

import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import javax.jms.JMSException;
import javax.jms.Message;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessagePostProcessor;


/**
 * Defines a singleton class to send  test steps over jms
 * @author nurhat
 */
public class TestStepDataObjectSender
{

    private static TestStepDataObjectSender m_sender;
    private JmsTemplate m_jmsTemplate;


    private TestStepDataObjectSender()
    {
    }


    /**
     * A static method for creating singleton instance. This method
     * used in  spring framework supported configuration files
     * to create an instance of this class to be used globally.
     * @param m_jmsTemplate for handling message destination  property.
     * Initially it is configured in  spring framework supported configuration files
     * @return
     */
    public static TestStepDataObjectSender createInstance(
        JmsTemplate jmsTemplate)
    {
        if (m_sender == null) {
            m_sender = new TestStepDataObjectSender();
            m_sender.m_jmsTemplate = jmsTemplate;
        }

        return m_sender;
    }


    public static TestStepDataObjectSender getInstance()
    {
        return m_sender;
    }


    /**
     * Sends a  TestStepDataObject over jms using setting configured spring framework supported
     * configuration files
     * @param data
     */
    public synchronized boolean sendData(final TestStepDataObject data)
    {
        LogSenderService.putMessageOnQueue("TestStepDataSender",
                                           AtofLogLevel.INFO,
                                           "Sending test step data object",
                                           data.getMessageId());

        try {
            m_jmsTemplate.convertAndSend(data,
                                       new MessagePostProcessor()
                                       {

                                           public Message postProcessMessage(
                                               Message message)
                                               throws JMSException
                                           {
                                               message.setJMSCorrelationID(
                                                   data.getMessageId());
                                               return message;
                                           }
                                       });
            return true;
        } catch (JmsException ex) {
            LogSenderService.putMessageOnQueue("TestStepDataSender",
                                               AtofLogLevel.ERROR,
                                               "Could not send test step data object..." +
                                               ex.getMessage(),
                                               data.getMessageId());
        }
        return false;
    }
}
